﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CommunitySystem.EmpbasicService;
using System.Collections.ObjectModel;
using CommunitySystem.JinShengService;
using EHR.Common;

namespace CommunitySystem.PageView
{
    public partial class JSCJWindow : ChildWindow
    {
        private Emp_CJJS entity = new Emp_CJJS();
        private JinShengServiceClient Client = new JinShengServiceClient();
        public JSCJWindow()
        {
            InitializeComponent();

            this.DataContext = entity;
        }
        /// <summary>
        /// 根据组织机构选择人
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        ObservableCollection<EmpbasicService.Emp_BasicInfoModel> SelectItems = new ObservableCollection<EmpbasicService.Emp_BasicInfoModel>();
        void btnSelname_Click(object sender, RoutedEventArgs e)
        {
            SelOrgSelEmpNameWindow wind = new SelOrgSelEmpNameWindow(SelectItems);
            wind.Show();
            wind.Closed += (sender1, e1) =>
            {
                if (wind.DialogResult == true && wind.SelectItems != null)
                {
                    SelectItems = new ObservableCollection<EmpbasicService.Emp_BasicInfoModel>();
                    foreach (var emp in wind.SelectItems)
                    {
                        if (SelectItems.Where(c => c.ID == emp.ID).Count() <= 0)
                        {
                            SelectItems.Add(new EmpbasicService.Emp_BasicInfoModel() { ID = emp.ID, Name = emp.Name, Birthday = emp.Birthday, PositionName = emp.PositionName, OrgName = emp.OrgName, Gender = emp.Gender, DutyName = emp.DutyName,DutyLevel=emp.DutyLevel,DutyAttribute=emp.DutyAttribute,CardNo=emp.CardNo });
                        }
                        else
                        {
                            //  SelectItems.Remove(emp);
                        }
                    }
                    //   lbDetail.ItemsSource = SelectItems;

                    if (SelectItems.Count > 0)
                    {
                        var selEmp = SelectItems.First();
                        entity.Eid = selEmp.ID;
                        entity.Name = selEmp.Name;
                        entity.CardNo = selEmp.CardNo;
                        entity.OldDW = selEmp.OrgName;
                        entity.OldKS = selEmp.PositionName;
                        entity.OldZW = selEmp.DutyName;
                        entity.OldZWCC = selEmp.DutyLevel;
                        entity.OldZWSX = selEmp.DutyAttribute;

                        this.tbName.Text = selEmp.Name;
                        this.tblCardNo.Text = string.IsNullOrEmpty(selEmp.CardNo)?"-":selEmp.CardNo;
                        this.tblOldDW.Text = string.IsNullOrEmpty(selEmp.OrgName) ? "-" : selEmp.OrgName;
                        this.tblOldZW.Text = string.IsNullOrEmpty(selEmp.DutyName) ? "-" : selEmp.DutyName;
                        this.tbOldKS.Text = string.IsNullOrEmpty(selEmp.PositionName) ? "-" : selEmp.PositionName;
                        this.tblOldZWCC.Text = string.IsNullOrEmpty(selEmp.DutyLevel) ? "-" : selEmp.DutyLevel;
                        this.tblOldZWSX.Text = string.IsNullOrEmpty(selEmp.DutyAttribute) ? "-" : selEmp.DutyAttribute;
                    }
                }
            };

        }
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(entity.Name))
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择人员!");
                return;
            }
            if (string.IsNullOrEmpty(entity.NewDW))
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择任职单位!");
                return;
            }
            if (string.IsNullOrEmpty(entity.BGRQ))
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择身份变更日期!");
                return;
            }
            if (string.IsNullOrEmpty(entity.DutyName))
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择职务名称!");
                return;
            }
            if (string.IsNullOrEmpty(entity.DutyLevel))
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择职务层次!");
                return;
            }
            if (string.IsNullOrEmpty(entity.DutyAttribute))
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择职务属性!");
                return;
            }
            //this.DialogResult = true;
            entity.CreateDate = DateTime.Now;
            entity.CreateUser = Session.LoginUser.LoginName;
            Client.SaveJYCompleted += new EventHandler<SaveJYCompletedEventArgs>(Client_SaveJYCompleted);
            Client.SaveJYAsync(entity, true);
        }

        void Client_SaveJYCompleted(object sender, SaveJYCompletedEventArgs e)
        {
            if (e.Result)
                SilverlightMessageBoxLibrary.Message.InfoMessage("保存成功！");

            else
                SilverlightMessageBoxLibrary.Message.InfoMessage("保存失败！");

            this.DialogResult = true;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
  
    }
}

